package org.liufeng.course.service;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RequestHtml {
	

	
		/**
		 * 发起http get请求获取网页源代码
		 * 
		 * @param requestUrl
		 * @return
		 */
		private static String httpRequest(String requestUrl) {
			StringBuffer buffer = null;

			try {
				// 建立连接
				URL url = new URL(requestUrl);
				HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
				httpUrlConn.setDoInput(true);
				httpUrlConn.setRequestMethod("GET");

				// 获取输入流
				InputStream inputStream = httpUrlConn.getInputStream();
				InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

				// 读取返回结果
				buffer = new StringBuffer();
				String str = null;
				while ((str = bufferedReader.readLine()) != null) {
					buffer.append(str);
				}

				// 释放资源
				bufferedReader.close();
				inputStreamReader.close();
				inputStream.close();
				httpUrlConn.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return buffer.toString();
		}

		/**
		 * 从html中抽取出历史上的今天信息
		 * 
		 * @param html
		 * @return
		 */
		private static String extract(String html) {
			StringBuffer buffer = new StringBuffer();
			Pattern p = Pattern.compile("(.*)(<DIV class=eventOverview>)(.*?)(</DIV>)(.*)");
			Matcher m = p.matcher(html);
			System.out.print(m.group(3));
			if (m.matches()) {
				 
			}
			// 将buffer最后两个换行符移除并返回
			return (null == buffer) ? null : buffer.substring(0, buffer.lastIndexOf("\n\n"));
		}


		/**
		 * 封装历史上的今天查询方法，供外部调用
		 * 
		 * @return
		 */
		public static String getTodayInHistoryInfo() {
			// 获取网页源代码
			String html = httpRequest("http://zt.zjmb.gov.cn/tfzt/db/CurTfInfoWap.jsp");
			// 从网页中抽取信息
			String result = extract(html);

			return result;
		}

		/**
		 * 通过main在本地测试
		 * 
		 * @param args
		 */
		public static void main(String[] args) {
			String info = getTodayInHistoryInfo();
		}
	}
